package cn.com.lyb.flink.window;

import cn.com.lyb.flink.bean.WaterSensor;
import cn.com.lyb.flink.split.WaterSensorMapFunction;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.ProcessingTimeSessionWindows;
import org.apache.flink.streaming.api.windowing.assigners.SessionWindowTimeGapExtractor;
import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

public class TimeWindowDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<WaterSensor> sensorDS = env.socketTextStream("172.32.1.6", 1234).map(new WaterSensorMapFunction());

        KeyedStream<WaterSensor, String> keyBy = sensorDS.keyBy((KeySelector<WaterSensor, String>) waterSensor -> waterSensor.getId());

        WindowedStream<WaterSensor, String, TimeWindow> window =
                keyBy.window(TumblingProcessingTimeWindows.of(Time.seconds(5))); // 滚动窗口，窗口长度10s
                // keyBy.window(SlidingProcessingTimeWindows.of(Time.seconds(10), Time.seconds(5))); // 滑动窗口 窗口长度10s 步长5s
        /**
         * 滑动窗口使用场景
         * 15s - 25s
         *    20s - 30s
         *        35s - 40s  里面有重叠的部分  每5秒输出一次最近10s的数据
         */
            //keyBy.window(ProcessingTimeSessionWindows.withGap(Time.seconds(5)));// 会话窗口，5秒内的没有数据进入的情况下数据为一组
           keyBy.window(ProcessingTimeSessionWindows.withDynamicGap(new SessionWindowTimeGapExtractor<WaterSensor>() {
               @Override
               public long extract(WaterSensor waterSensor) {
                   // 从数据提取ts 作为间隔 单位是毫秒,这个间隔会一直变化，为0的话此组数据已经结束
                   return waterSensor.getTs() * 1000L;
               }
           }));


                // 窗口分配器
//        window.apply(new WindowFunction<WaterSensor, Object, String, TimeWindow>() {
//            /**
//             *
//             *
//             * @param s 分组的key
//             * @param timeWindow 窗口对象
//             * @param iterable 存的数据
//             * @param collector 采集器
//             * @throws Exception
//             */
//            @Override
//            public void apply(String s, TimeWindow timeWindow, Iterable<WaterSensor> iterable, Collector<Object> collector) throws Exception {
//
//            }
//        }).print();

        window.process(new ProcessWindowFunction<WaterSensor, Object, String, TimeWindow>() {
            /**
             *
             * @param s 分组的key
             * @param context 上下文
             * @param iterable 迭代器
             * @param collector 采集器
             * @throws Exception
             */
            @Override
            public void process(String s, ProcessWindowFunction<WaterSensor, Object, String, TimeWindow>.Context context, Iterable<WaterSensor> iterable, Collector<Object> collector) throws Exception {
                long start = context.window().getStart();
                long end = context.window().getEnd();
                String startFormat = DateFormatUtils.format(start, "yyyy-MM-dd HH:mm:ss.SSS");
                String endFormat = DateFormatUtils.format(end, "yyyy-MM-dd HH:mm:ss.SSS");
                long cnt = iterable.spliterator().estimateSize();
                collector.collect("key = "+ s +"窗口开始时间 +" + startFormat +"结束时间" + endFormat +"迭代器大小" + cnt);
            }
        }).print();


        env.execute();
    }
}
